//
//  EditViewController.m
//  通讯录-02
//
//  Created by 伍银荣 on 2021/6/29.
//  Copyright © 2021 com.example. All rights reserved.
//

#import "EditViewController.h"
#import "Contact.h"

@interface EditViewController ()

@property (strong, nonatomic) IBOutlet UITextField *nameTF;
@property (strong, nonatomic) IBOutlet UITextField *numberTF;
@property (strong, nonatomic) IBOutlet UIButton *saveBtn;
@property (strong, nonatomic) IBOutlet UIBarButtonItem *editItem;

@end

@implementation EditViewController
- (IBAction)saveBtnClicked:(UIButton *)sender {
    if ([self.delegate respondsToSelector:@selector(editViewController:)]) {
        [self.delegate editViewController:self];
    }
    //获取恢复后的数据
    self.contact.name = self.nameTF.text;
    self.contact.number = self.numberTF.text;
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)editItemClicked:(UIBarButtonItem *)sender {
    if (self.saveBtn.hidden) {
        sender.title = @"取消";
        self.saveBtn.hidden = NO;
        self.nameTF.enabled = YES;
        self.numberTF.enabled = YES;
        [self.numberTF becomeFirstResponder];
        //重新获取编辑后的文本框内容
        self.contact.name = self.nameTF.text;
        self.contact.number = self.numberTF.text;
    } else {
        sender.title = @"编辑";
        self.saveBtn.hidden = YES;
        self.nameTF.enabled = NO;
        self.numberTF.enabled = NO;
    }
}



- (void)viewDidLoad {
    [super viewDidLoad];
    self.nameTF.text = self.contact.name;
    self.numberTF.text = self.contact.number;
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


@end
